package com.xk.handler.security;

import com.alibaba.fastjson.JSON;
import com.xk.domain.ResponseResult;
import com.xk.enums.AppHttpCodeEnum;
import com.xk.utils.WebUtils;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 自定义 授权失败异常处理器
 */
@Component
public class AccessDeniedHandlerImpl implements AccessDeniedHandler {
    /**
     *授权失败后 ,给用户提示
     */
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException)
            throws IOException, ServletException {
        System.out.println("---授权失败异常处理器----");
        accessDeniedException.printStackTrace();
        //提示错误信息 响应给前端
        ResponseResult result = ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH);
        WebUtils.renderString(response, JSON.toJSONString(result));

    }
}
